﻿/*更新内容
1、AppNative.prototype.backPage 的 页面跳转中
2、刷新界面
3、打开选择拍照和相册界面
4、//获取 app版本号
5、消息条数减少或者清零 大于0的数字为减少的条数 传0 则直接清零
6、隐藏APP键盘
*/
;
(function (global) {

    _isAppNative = function () {
        return _type == 1 || _type == 2;
    }

    _isWeixin = function () {
        return _type == 5;
    }

    function AppNative() {

        this.isAppNative = _isAppNative;
        this.isWeixin = _isWeixin;
    }

    //页面跳转
    AppNative.prototype.gotoPage = function (url) {
        url = url.replace(/\s+/g, "");

        if (_isAppNative() && global.MallcooJavaScriptInterface && global.MallcooJavaScriptInterface.openPage) {
            MallcooJavaScriptInterface.openPage(url);
            //window.location.href = "mallcooapp://webview/openpage?url=" + encodeURIComponent(url);
        }
        else {
            window.location.href = url;
        }
    }

    //刷新页面
    AppNative.prototype.pageRefresh = function (refreshUrl) {
        if (_isAppNative() && global.MallcooJavaScriptInterface && global.MallcooJavaScriptInterface.pageRefresh) {
            try {
                MallcooJavaScriptInterface.pageRefresh(refreshUrl);
            } catch (ex) {
                alert(ex);
            }
        } else {
            window.location.href = refreshUrl;
        }
    }

    //后退
    AppNative.prototype.backPage = function () {
        if (_isAppNative() && global.MallcooJavaScriptInterface && global.MallcooJavaScriptInterface.pageExit) {
            try {
                MallcooJavaScriptInterface.pageExit();
            }
            catch (ex) {
                alert(ex);
            }
            //window.location.href = "mallcooapp://webview/exit";
        }
    }

    //隐藏APP键盘
    AppNative.prototype.onHiddenKeyBoard = function () {
        if (_isAppNative() && global.MallcooJavaScriptInterface && global.MallcooJavaScriptInterface.onHiddenKeyBoard) {
            try {
                MallcooJavaScriptInterface.onHiddenKeyBoard();
            }
            catch (ex) { alert(ex); }
        }
    }

    //#endregion

    //#region 支付

    //支付
    AppNative.prototype.pay = {
        //微信支付
        weixinpay: function (payurl, redirecturl) {
            if (_isAppNative()) {
                //window.location.href = "mallcooapp://pay/weixinpay?payurl=" + payurl + "&redirecturl=" + redirecturl;
                try {
                    MallcooJavaScriptInterface.onWeixinPay(payurl, redirecturl);
                }
                catch (ex) {
                    alert(ex);
                }
            }
            else {
                alert("暂不支持")
            }
        },
        //支付宝支付
        alipay: function (payurl, redirecturl) {
            if (_isAppNative()) {
                //window.location.href = "mallcooapp://pay/alipay?payurl=" + payurl + "&redirecturl=" + redirecturl;
                try {
                    MallcooJavaScriptInterface.onAliPay(payurl, redirecturl);
                }
                catch (ex) {
                    alert(ex);
                }
            }
            else {
                alert("暂不支持")
            }
        }
    };

    //#endregion

    //#region 设备接口

    //设备接口
    AppNative.prototype.device = {
        //注册摇一摇事件
        shake: function (readycallbackName, shakecallbackName) {
            if (_isAppNative()) {
                try {
                    //window.location.href = "mallcooapp://device/shake?readycallback=" + readycallback + "&shakecallback=" + shakecallback;
                    MallcooJavaScriptInterface.deviceShake(readycallbackName, shakecallbackName);
                }
                catch (ex) {
                    alert(ex);
                }
            }
            else {
                alert("暂不支持")
            }
        },
        //停止摇一摇
        stopshake: function () {

            if (_isAppNative()) {
                try {
                    //window.location.href = "mallcooapp://device/stopshake";
                    MallcooJavaScriptInterface.stopShake();
                }
                catch (ex) {
                    alert(ex);
                }
            }
            else {
                alert("暂不支持")
            }
        },
        //重启摇一摇
        restartshake: function () {

            if (_isAppNative()) {
                try {
                    //window.location.href = "mallcooapp://device/restartshake";
                    MallcooJavaScriptInterface.restartShake();
                }
                catch (ex) {
                    alert(ex);
                }
            }
            else {
                alert("暂不支持")
            }
        },

        //打电话
        phoneCall: function (number) {
            if (_isAppNative()) {
                try {
                    //window.location.href = "mallcooapp://device/call?number=" + number;
                    MallcooJavaScriptInterface.callPhone(number + "");
                }
                catch (ex) {
                    alert(ex);
                }
            }
        },

        //二维码扫描
        qrScan: function (qrCallbackName) {

            if (_isAppNative()) {
                try {
                    MallcooJavaScriptInterface.onQrScan('扫二维码', qrCallbackName);
                }
                catch (ex) {
                    alert(ex);
                }
            }
            else {
                alert("暂不支持")
            }
        },
        //拍照
        opendCamera: function (callbackName) {
            if (_isAppNative() && global.MallcooJavaScriptInterface && global.MallcooJavaScriptInterface.opendCamera) {
                try {
                    MallcooJavaScriptInterface.opendCamera(callbackName);
                }
                catch (ex) {
                    alert(ex);
                }
            }
        }
    };


    //#endregion



    //#region 用户 （目前只有 项目版APP登录 ）

    AppNative.prototype.user = {
        //登录
        login: function (url) {
            url = url.replace(/\s+/g, "");
            if (_isAppNative()) {
                window.location.href = "mallcooapp://user/login?redirecturl=" + encodeURIComponent(url);
            }
        },
        //注册
        register: function (url) {
            url = url.replace(/\s+/g, "");
            if (_isAppNative()) {
                window.location.href = "mallcooapp://user/register?redirecturl=" + encodeURIComponent(url);
            }
        }
    };

    //#endregion

    //#region 切换商场

    /*切换商场
    *@ @ mid 商场MID
    */
    AppNative.prototype.resetMallId = function (mid) {
        if (_isAppNative()) {
            try {
                MallcooJavaScriptInterface.resetMallId(mid);
            }
            catch (ex) {
                alert(ex);
            }
        }
        else {
            alert("暂不支持")
        }
    };

    //#endregion

    //#region 返回首页

    /*
    *@ goBackHome 返回首页
    */
    AppNative.prototype.goBackHome = function (mid) {
        mid = mid || null;
        if (_isAppNative()) {
            try {
                MallcooJavaScriptInterface.goBackHome(mid);
            }
            catch (ex) {
                alert(ex);
            }
        }
        else {
            window.location.href = $HomeUrl;
        }
    }

    //#endregion

    //#region Header与app对接

    //AppNative.prototype.initHeader = {
    //    right: function (title, isLine, btnTxt1, btnImg1, callBack1, btnTxt2, btnImg2, callBack2, titleBtnCallback) {
    //        title = title || "";
    //        isLine = isLine || "";
    //        btnTxt1 = btnTxt1 || "";
    //        btnImg1 = btnImg1 || "";
    //        callBack1 = callBack1 || "";
    //        btnTxt2 = btnTxt2 || "";
    //        btnImg2 = btnImg2 || "";
    //        callBack2 = callBack2 || "";
    //        titleBtnCallback = titleBtnCallback || "";
    //        if (_isAppNative()) {
    //            window.location.href = "mallcooApp://webview/initHeader?title=" + encodeURIComponent(title)
    //                                    + "&haveLine=" + encodeURIComponent(isLine)
    //                + "&rightBtnTxt=" + encodeURIComponent(btnTxt1) + "&rightBtnImg=" + encodeURIComponent(btnImg1) + "&rightBtnCallback=" + callBack1
    //              + "&rightBtn2Txt=" + encodeURIComponent(btnTxt2) + "&rightBtn2Img=" + encodeURIComponent(btnImg2) + "&rightBtn2Callback=" + callBack2
    //            + "&titleBtnCallback=" + titleBtnCallback;
    //        }
    //    }
    //}

    AppNative.prototype.header = {
        /*
        * 传入参数
        *@ txt 文字内容
        *@ callbackName 点击之后调用的js方法
        */
        rightText: function (txt, callbackName) {
            if (_isAppNative()) {
                MallcooJavaScriptInterface.onHeaderRightText(txt, callbackName);
            }
        },

        /*
        * 传入参数
        *@ rightStr 文字内容
        *@ http://i1.mallcoo.cn//mc/xxx.jph 图标地址
        *@ onHeaderRightTextWithImgResultJavaScript 点击之后调用的js方法
        *@ imgLocation 0,左边 1,右边
        */
        rightTextWithImg: function (txt, url, callbackName, imgLocation) {
            if (_isAppNative()) {
                MallcooJavaScriptInterface.onHeaderRightTextWithImg(txt, url, callbackName, imgLocation);
            }
        },
        /*
        * 传入参数
        *@ http://i1.mallcoo.cn//mc/xxx.jph 图标地址
        *@ onHeaderRightImageResultJavaScript 点击之后调用的js方法
        */
        onHeaderRightImage: function (imgUrl, callback) {
            if (_isAppNative() && MallcooJavaScriptInterface && MallcooJavaScriptInterface.onHeaderRightImage) {
                MallcooJavaScriptInterface.onHeaderRightImage(imgUrl, callback);
            }
        },
        /*
        * 传入参数
        *@ htmlStr 文字内容
        *@ onHeaderLeftTextResultJavaScript 点击之后调用的js方法
        * htmlStr例（良食局首页）：<big><big><font color="#DF380F">天津</font></big></big><font color="#ffffff">良食局</font>
        */
        onHeaderLeftText: function (htmlStr, callback) {
            if (_isAppNative() && MallcooJavaScriptInterface && MallcooJavaScriptInterface.onHeaderLeftText) {
                MallcooJavaScriptInterface.onHeaderLeftText(htmlStr, callback);
            }
        },

        /*
        * 传入参数
        *@ 图标地址
        *@ onHeaderLeftImage 点击之后调用的js方法
        */
        onHeaderLeftImage:function(imgUrl, callback){
            if(_isAppNative() && MallcooJavaScriptInterface && MallcooJavaScriptInterface.onHeaderLeftImage){
                MallcooJavaScriptInterface.onHeaderLeftImage(imgUrl, callback);
            }
        },

        /*
        * 传入参数
        *@ htmlStr 文字内容
        *@ onHeaderLeftTextResultJavaScript 点击之后调用的js方法
        * htmlStr例（良食局首页）：<big><big><font color="#DF380F">天津</font></big></big><font color="#ffffff">良食局</font>
        */
        onHeaderTitle: function (htmlStr, titleImg, callback) {
            if (_isAppNative() && MallcooJavaScriptInterface && MallcooJavaScriptInterface.onHeaderTitle) {
                MallcooJavaScriptInterface.onHeaderTitle(htmlStr, titleImg, callback);
            }
        }
    };

    //#endregiongo

    //#region 搜索页面

    /*
    *@ openSearchPage 打开搜索页面
    *传入参数
    *@ option {url(搜索页面地址),hintstring(搜索框hint占位内容),rightstring(右边文字按钮内容),rightimgstring(右边图标按钮链接),suggestjavascript(suggest调用的js方法),searchjavascript(搜索调用的js方法)}(具体参照下面写法)
    *返回参数 
    *@ suggestStr suggest内容
    *@ searchStr 搜索内容
    */
    AppNative.prototype.openSearchPage = function (option) {
        option.url = option.url || "";
        option.hintstring = option.hintstring || "";
        option.rightstring = option.rightstring || "";
        option.rightimgstring = option.rightimgstring || "";
        option.suggestjavascript = option.suggestjavascript || "";
        option.searchjavascript = option.searchjavascript || "";

        if (_isAppNative()) {
            //{\"url\":\"http://gis.api.mallcoo.cn/backup/lydw/calibrationPoints/search.html\",\"hintstring\":\"写点什么吧\",\"rightstring\":\"搜索\",\"rightimgstring\":\"\",\"suggestjavascript\":\"suggestjavascript\",\"searchjavascript\":\"searchjavascript\"};
            try {
                MallcooJavaScriptInterface.openSearchPage(option);
            }
            catch (ex) {
                alert(ex);
            }
        }
    }

    //#endregion

    //#region user

    //登录设置token
    AppNative.prototype.onUserLogin = function (userToken) {

        if (_isAppNative() && global.MallcooJavaScriptInterface && global.MallcooJavaScriptInterface.onUserLogin) {
            try {
                MallcooJavaScriptInterface.onUserLogin(userToken);
            }
            catch (ex) {
                alert(ex);
            }
        }
    }


    AppNative.prototype.onUserLoginV2 = function (userInfo) {
        if (_isAppNative() && global.MallcooJavaScriptInterface && global.MallcooJavaScriptInterface.onUserLoginV2) {
            try {
                MallcooJavaScriptInterface.onUserLoginV2(JSON.stringify(userInfo));
            }
            catch (ex) {
                alert(ex);
            }
        }
        else {
            var userToken = userInfo.UserToken;
            appNative.onUserLogin(userToken);
        }
    }

    //登出
    AppNative.prototype.onUserLogout = function () {

        if (_isAppNative() && global.MallcooJavaScriptInterface && global.MallcooJavaScriptInterface.onUserLogout) {
            try {
                MallcooJavaScriptInterface.onUserLogout();
            }
            catch (ex) {
                alert(ex);
            }
        }
    }

    //打开选择 拍照 和 相册 界面
    AppNative.prototype.openAlbumAndCameraChoosePage = function (chooseStr) {
        if (_isAppNative() && global.MallcooJavaScriptInterface && global.MallcooJavaScriptInterface.openAlbumAndCameraChoosePage) {
            try {
                MallcooJavaScriptInterface.openAlbumAndCameraChoosePage(chooseStr);
            } catch (ex) {
                alert(ex);
            }
        }
    }

    //#endregion

    //#region 分享
    /*
    * 传入参数
    *@ sr 新浪分享内容
    *@ wr 微信分享内容
    *@ wu 微信转发链接
    *@ au QQ分享内容
    *@ imgUrl 分享的图片链接地址
    *@ title 微信分享标题
    *@ callbackName 分享成功后调用的js方法名称
    */
    AppNative.prototype.webview = {
        //分享（sr:新浪微博转发内容,wr:微信转发内容,wu|微信转发链接,au|app下载地址）
        share: function (sr, wr, wu, au, imgUrl, title, callbackName) {
            imgUrl = imgUrl || "";
            sr = sr || "";
            wr = wr || "";
            wu = wu || "";
            au = au || "";
            title = title || "";
            if (_isAppNative()) {
                //window.location.href = "mallcooapp://webview/share?sr=" + encodeURIComponent(sr)
                //    + "&wr=" + encodeURIComponent(wr)
                //    + "&wu=" + encodeURIComponent(wu)
                //    + "&au=" + encodeURIComponent(au)
                //    + "&imgUrl=" + encodeURIComponent(imgUrl)
                //;
                try {
                    MallcooJavaScriptInterface.webviewShare(sr, wr, wu, au, imgUrl, title, callbackName);
                }
                catch (ex) {
                    alert(ex);
                }
            }
            else {
                // 分享弹出框
                $.Zebra_Dialog($("#sharebox").html(), { 'buttons': false });
                $('.sharebox').show();
                $('.showbox_mask').hide();
                $('#commShowBox').hide();
            }
        }
    };

    //#endregion

    //#region 首页搜索头部

    //传入参数
    //@ searchJsonString {url(搜索页面地址),hintstring(搜索框hint占位内容),rightstring(右边文字按钮内容),rightimgstring(右边图标按钮链接),suggestjavascript(suggest调用的js方法),searchjavascript(搜索调用的js方法),rightstringjavascript（右边文字js方法）}(注意是json格式的字符串)
    //"{\"url\":\"http://gis.api.mallcoo.cn/backup/lydw/calibrationPoints/search.html\",\"hintstring\":\"写点什么吧\",\"rightstring\":\"搜索\",\"rightimgstring\":\"\",\"suggestjavascript\":\"suggestjavascript\",\"rightstringjavascript\":\"rightstringjavascript\",\"searchjavascript\":\"searchjavascript\"}"
    //返回参数 
    //suggestStr suggest内容
    //searchStr 搜索内容
    AppNative.prototype.createSearchHeader = function (initSearchContent) {
        if (_isAppNative() && global.MallcooJavaScriptInterface && global.MallcooJavaScriptInterface.createSearchHeader) {
            try {
                MallcooJavaScriptInterface.createSearchHeader(JSON.stringify(initSearchContent));
            }
            catch (ex) {
                alert(ex);
            }
        }
    };
    //#endregion


    //获取 app版本号
    AppNative.prototype.getAppVersion = function (getAppVersion) {
        if (_isAppNative() && global.MallcooJavaScriptInterface && global.MallcooJavaScriptInterface.getAppVersion) {
            try {
                MallcooJavaScriptInterface.getAppVersion(getAppVersion);
            }
            catch (ex) {
                alert(ex);
            }
        }
    }


    //消息条数减少或者清零 大于0的数字为减少的条数 传0 则直接清零
    AppNative.prototype.messageCountReduceOrClear = function (count) {
        if (_isAppNative() && global.MallcooJavaScriptInterface && global.MallcooJavaScriptInterface.messageCountReduceOrClear) {
            try {
                MallcooJavaScriptInterface.messageCountReduceOrClear(count);
            } catch (ex) {
                alert(ex);
            }
        }
    }

    global.appNative = new AppNative();
})(window);
